package com.pond.pond.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.pond.common.core.utils.DateUtils;
import com.pond.pond.domain.FinanceRecord;
import com.pond.pond.domain.vo.FishFlowRecordVo;
import com.pond.pond.mapper.FinanceRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pond.pond.mapper.FishFlowRecordMapper;
import com.pond.pond.domain.FishFlowRecord;
import com.pond.pond.service.IFishFlowRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 鱼流转记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@Service
@Transactional
public class FishFlowRecordServiceImpl implements IFishFlowRecordService 
{
    @Autowired
    private FishFlowRecordMapper fishFlowRecordMapper;

    @Autowired
    private FinanceRecordMapper financeRecordMapper;
    /**
     * 查询鱼流转记录
     * 
     * @param id 鱼流转记录主键
     * @return 鱼流转记录
     */
    @Override
    public FishFlowRecordVo selectFishFlowRecordById(Long id)
    {
        return fishFlowRecordMapper.selectFishFlowRecordById(id);
    }

    /**
     * 查询鱼流转记录列表
     * 
     * @param fishFlowRecord 鱼流转记录
     * @return 鱼流转记录
     */
    @Override
    public List<FishFlowRecordVo> selectFishFlowRecordList(FishFlowRecordVo fishFlowRecord)
    {
        return fishFlowRecordMapper.selectFishFlowRecordList(fishFlowRecord);
    }

    @Override
    public List<FishFlowRecord> selectFishFlowRecordList1(FishFlowRecord fishFlowRecord)
    {
        return fishFlowRecordMapper.selectFishFlowRecordList1(fishFlowRecord);
    }

    /**
     * 新增鱼流转记录
     * 
     * @param fishFlowRecord 鱼流转记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFishFlowRecord(FishFlowRecord fishFlowRecord) {
        fishFlowRecord.setCreateTime(DateUtils.getNowDate());
        int result = fishFlowRecordMapper.insertFishFlowRecord(fishFlowRecord);


        System.out.println("type"+fishFlowRecord.getFlowType());
        if (result > 0 && Integer.valueOf(1).equals(fishFlowRecord.getBindFinanceRecord())) {
            // 只对进鱼和出鱼生成财务记录，转塘不生成
            if (fishFlowRecord.getFlowType() != null && (fishFlowRecord.getFlowType() == 1 || fishFlowRecord.getFlowType() == 2)) {
                FinanceRecord financeRecord = new FinanceRecord();
                financeRecord.setDeptId(fishFlowRecord.getDeptId());

                BigDecimal weight = new BigDecimal(String.valueOf(fishFlowRecord.getWeight()));
                BigDecimal pricePerKg = new BigDecimal(String.valueOf(fishFlowRecord.getPricePerKg()));
                BigDecimal amount = weight.multiply(pricePerKg);
                System.out.println("amount"+amount);
                financeRecord.setAmount(amount);

                if (fishFlowRecord.getFlowType() == 1) { // 进鱼，买鱼款，支出
                    financeRecord.setType("EXPENSE");
                    financeRecord.setDescription("买鱼款");
                } else if (fishFlowRecord.getFlowType() == 2) { // 出鱼，卖鱼款，收入
                    financeRecord.setType("INCOME");
                    financeRecord.setDescription("卖鱼款");
                }

                financeRecord.setSourceType("FISH_FLOW");
                financeRecord.setSourceId(fishFlowRecord.getId());
                financeRecord.setTime(new Date());
                financeRecord.setCreateBy(fishFlowRecord.getCreateBy());

                int financeResult = financeRecordMapper.insertFinanceRecord(financeRecord);
                if (financeResult > 0) {
                    fishFlowRecord.setLinkedFinanceId(financeRecord.getId());
                    fishFlowRecordMapper.updateFishFlowRecord(fishFlowRecord);
                } else {
                    throw new RuntimeException("财务记录插入失败");
                }
            }
        }

        return result;
    }

    private static FinanceRecord getFinanceRecord(FishFlowRecord fishFlowRecord) {
        FinanceRecord financeRecord = new FinanceRecord();
        financeRecord.setDeptId(fishFlowRecord.getDeptId());
        financeRecord.setAmount(fishFlowRecord.getWeight().multiply(new BigDecimal("10"))); // 示例金额计算
        financeRecord.setType("EXPENSE");
        financeRecord.setSourceType("FISH_FLOW");
        financeRecord.setSourceId(fishFlowRecord.getId());  // 确认不为 null
        financeRecord.setDescription("鱼流转支出");
        financeRecord.setTime(new Date());
        financeRecord.setCreateBy(fishFlowRecord.getCreateBy());
        return financeRecord;
    }


    /**
     * 修改鱼流转记录
     * 
     * @param fishFlowRecord 鱼流转记录
     * @return 结果
     */
    @Override
    public int updateFishFlowRecord(FishFlowRecord fishFlowRecord)
    {
        fishFlowRecord.setUpdateTime(DateUtils.getNowDate());
        return fishFlowRecordMapper.updateFishFlowRecord(fishFlowRecord);
    }

    /**
     * 批量删除鱼流转记录
     * 
     * @param ids 需要删除的鱼流转记录主键
     * @return 结果
     */
    @Override
    public int deleteFishFlowRecordByIds(Long[] ids)
    {
        return fishFlowRecordMapper.deleteFishFlowRecordByIds(ids);
    }

    /**
     * 删除鱼流转记录信息
     * 
     * @param id 鱼流转记录主键
     * @return 结果
     */
    @Override
    public int deleteFishFlowRecordById(Long id)
    {
        return fishFlowRecordMapper.deleteFishFlowRecordById(id);
    }
}
